import pandas as pd
import pymysql
from sqlalchemy import create_engine

# 创建一个数据框
data = {
    'id': [1, 2, 3, 4, 5],
    'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
    'age': [25, 30, 35, 40, 45]
}
df = pd.DataFrame(data)

# MySQL数据库连接信息
db_host = 'localhost'
db_user = 'root'
db_password = '123456'
db_name = 'test'

# 创建MySQL连接
engine = create_engine(f'mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}')

# 将数据框写入MySQL数据库中的表
df.to_sql('funds', con=engine, if_exists='replace', index=False)

# 关闭连接
engine.dispose()
